/**
 * 
 */
package com.huarui.desktop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.huarui.desktop.dao.TableDataAccessException;
import com.huarui.desktop.dao.TableDataDao;
import com.huarui.desktop.model.TableColumnInfo;
import com.huarui.desktop.model.TableMetaData;

/**
 * @author sunpeng
 * 
 */
public class TableDataDaoImpl extends SqlMapClientDaoSupport implements TableDataDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.desktop.dao.TableDataDao#selectAllData(com.huarui.desktop.
	 * model.TableMetaData, int, int)
	 */
	@Override
	public List<Map<String, Object>> selectAllData(TableMetaData metaData, int start, int limit) {
		ResultSet resultSet = null;
		PreparedStatement pstmt = null;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			Connection con = super.getSqlMapClientTemplate().getDataSource().getConnection();
			pstmt = con.prepareStatement("select * from " + metaData.getName() + " order by ? limit ?,? ");
			pstmt.setString(1, metaData.getColumns()[0].getName());
			pstmt.setInt(2, start);
			pstmt.setInt(3, limit);
			resultSet = pstmt.executeQuery();
			while (resultSet.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				for (TableColumnInfo column : metaData.getColumns()) {
					map.put(column.getName(), resultSet.getObject(column.getName()));
				}
				list.add(map);
			}
		} catch (SQLException e) {
			throw new TableDataAccessException("读取库表记录发生错误。",e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					throw new TableDataAccessException("读取库表记录发生错误。",e);
				}
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					throw new TableDataAccessException("读取库表记录发生错误。",e);
				}
			}

		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.huarui.desktop.dao.ModuleDataDao#selectCount(java.lang.String)
	 */
	@Override
	public int selectCount(String tableName) {
		ResultSet resultSet = null;
		PreparedStatement pstmt = null;
		int total = 0;
		try {
			Connection con = super.getSqlMapClientTemplate().getDataSource().getConnection();
			pstmt = con.prepareStatement("select count(1) as total from " + tableName);

			resultSet = pstmt.executeQuery();
			if (resultSet.next()) {
				total = resultSet.getInt("total");
			}
		} catch (SQLException e) {
			throw new TableDataAccessException("读取库表记录发生错误。",e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					throw new TableDataAccessException("读取库表记录发生错误。",e);

				}
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					throw new TableDataAccessException("读取库表记录发生错误。",e);

				}
			}

		}
		return total;
	}

}
